package com.zz.xsz.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zz.xsz.domain.CourseDO;
import com.zz.xsz.service.CourseService;
import com.zz.common.utils.R;

/**
 * 课程管理
 * 
 * @author bsea
 * @email yinyouhai@aliyun.com
 * @date 2020-10-13 22:53:49
 */
 
@Controller
@RequestMapping("/xsz/course")
public class CourseController {
	@Autowired
	private CourseService courseService;
	
	@GetMapping()
	@RequiresPermissions("xsz:course:course")
	String Course(){
	    return "xsz/course/course";
	}

	@GetMapping("courseList")
	@RequiresPermissions("xsz:course:course")
	String toCourseList(){
		return "xsz/course/courseList";
	}
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("xsz:course:course")
	public List<CourseDO> list(@RequestParam Map<String, Object> params){
		//查询列表数据
		List<CourseDO> courseDOS = courseService.list(params);
		return courseDOS;

	}
	@GetMapping("/add/{pId}")
	@RequiresPermissions("xsz:course:add")
	String add(Model model, @PathVariable("pId") Long pId)
	{
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "训练课程");
		} else {
			model.addAttribute("pName", courseService.get(pId).getName());
		}

	    return "xsz/course/add";
	}

	@GetMapping("/edit/{courseId}")
	@RequiresPermissions("xsz:course:edit")
	String edit(@PathVariable("courseId") Long courseId,Model model){
		CourseDO course = courseService.get(courseId);
		model.addAttribute("course", course);
	    return "xsz/course/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("xsz:course:add")
	R save(CourseDO courseDO) {

		if (courseService.save(courseDO) > 0) {
			return R.ok();
		} else {
			return R.error(1, "保存失败");
		}
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("xsz:course:edit")
	public R update( CourseDO course){
		courseService.update(course);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("xsz:course:remove")
	public R remove( Long courseId){
		if(courseService.remove(courseId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("xsz:course:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] courseIds){
		courseService.batchRemove(courseIds);
		return R.ok();
	}
	
}
